##  program:  results.rplots.r
##  task:  	  graphs of experiment results
## 	input:	  results-q1-r.csv, results-q2-r.csv, results-q3-r.csv, results-q4-r.csv					
##	output:		q1.tiff, q2.tiff, q3.tiff, q4.tiff
##  project:  MEDEC experiment 1
##  author:   sam harper \ 25mar2013

setwd(".......")
library("ggplot2")
library("foreign")
library("gridExtra")

################
## Question 1 ##
################

# Need to set working directory
# Assumes data files are in working directory

results <- read.csv("results-q1-r.csv")

# Subset by inconsistent vs. consistent scenarios
results1 <-subset(results,scen<4)
results2 <-subset(results,scen>3)

# Define the top and bottom of the errorbars
limits <- aes(ymax = u95, ymin=l95)

# Labels for time
results$time <- factor(results$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results1$time <- factor(results1$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results2$time <- factor(results2$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))

# Labels for scenario
results$scen <- factor(results$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results1$scen <- factor(results1$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results2$scen <- factor(results2$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

# All scenarios on single plot
ggplot(results, aes(fill=as.factor(trt), y=margin, x=as.factor(time))) + geom_bar(position="dodge", stat="identity", colour="black") + geom_errorbar(limits, position = position_dodge(width=0.9), width=0.2) + scale_fill_manual(name="Experimental Condition",values=c("#CCCCCC","#FFFFFF"),labels=c("Shown raw data and difference measure of inequality","Shown raw data and ratio measure of inequality")) + facet_wrap(~scen, nrow=2) + theme_bw() + theme(legend.position="bottom", legend.direction="vertical") + ylab("Probability of agreement with ratio measure of inequality") + xlab("") + theme(axis.text.x = element_text(size = 9), axis.title.y=element_text(size=10, angle=90), axis.text.y = element_text(size = 9), strip.text = element_text(size=7))

ggsave("q1.tiff", width=6, height=5, dpi=600)



################
## Question 2 ##
################

results <- read.csv("results-q2-r.csv")

# Subset by inconsistent vs. consistent scenarios
results1 <-subset(results,scen<4)
results2 <-subset(results,scen>3)

# Define the top and bottom of the errorbars
limits <- aes(ymax = u95, ymin=l95)

# Labels for time
results$time <- factor(results$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results1$time <- factor(results1$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results2$time <- factor(results2$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))

# Labels for scenario
results$scen <- factor(results$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results1$scen <- factor(results1$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results2$scen <- factor(results2$scen, levels=c('1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant, ratio increases','Difference decreases, ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

# All scenarios on single plot
ggplot(results, aes(fill=as.factor(trt), y=margin, x=as.factor(time))) + geom_bar(position="dodge", stat="identity", colour="black") + scale_y_continuous(limits=c(0,7)) + geom_errorbar(limits, position = position_dodge(width=0.9), width=0.2) + scale_fill_manual(name="Experimental Condition",values=c("#CCCCCC","#FFFFFF"),labels=c("Shown raw data and difference measure of inequality","Shown raw data and ratio measure of inequality")) + facet_wrap(~scen, nrow=2) + theme_bw() + theme(legend.position="bottom", legend.direction="vertical") + ylab("How successful in reducing inequality? \n 1 (Not at all) ... 7 (Very successful)") + xlab("") + theme(axis.text.x = element_text(size = 9), axis.title.y=element_text(size=10, angle=90), axis.text.y = element_text(size = 9), strip.text = element_text(size=7))

ggsave("q2.tiff", width=6, height=5, dpi=600)



################
## Question 3 ##
################

results <- read.csv("results-q3-r.csv")

# Subset by inconsistent vs. consistent scenarios
results1 <-subset(results,scen<4)
results2 <-subset(results,scen>3)

# Define the top and bottom of the errorbars
limits <- aes(ymax = u95, ymin=l95)

# Labels for time
results$time <- factor(results$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results1$time <- factor(results1$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results2$time <- factor(results2$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))

# Labels for scenario
results$scen <- factor(results$scen, levels=c('0','1','2','3','4','5'), labels=c('Diff. decreases, Small ratio increase','Diff. decreases, Large ratio increase','Difference constant,ratio increases','Difference decreases,ratio constant','Difference decreases,ratio decreases', 'Difference increases,ratio increases'))

results1$scen <- factor(results1$scen, levels=c('0','1','2','3','4','5'), labels=c('Diff. decreases, \n small ratio increase','Diff. decreases, \n large ratio increase','Difference constant, \n ratio increases','Difference decreases, \n ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results2$scen <- factor(results2$scen, levels=c('0','1','2','3','4','5'), labels=c('Diff. decreases, \n small ratio increase','Diff. decreases, \n large ratio increase','Difference constant, \n ratio increases','Difference decreases, \n ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

# All scenarios on single plot
ggplot(results, aes(fill=as.factor(trt), y=margin, x=as.factor(time))) + geom_bar(position="dodge", stat="identity", colour="black") + scale_y_continuous(limits=c(0,100)) + geom_errorbar(limits, position = position_dodge(width=0.9), width=0.2) + scale_fill_manual(name="Experimental Condition",values=c("#CCCCCC","#FFFFFF"),labels=c("Shown raw data and difference measure of inequality","Shown raw data and ratio measure of inequality")) + facet_wrap(~scen, nrow=2) + theme_bw() + theme(legend.position="bottom", legend.direction="vertical") + ylab("How much to support program continuation? \n Min ($0) ... Max ($100)") + xlab("") + theme(axis.text.x = element_text(size = 9), axis.title.y=element_text(size=10, angle=90), axis.text.y = element_text(size = 9), strip.text = element_text(size=7)) 

ggsave("q3.tiff", width=6, height=5, dpi=600)



################
## Question 4 ##
################

results <- read.csv("results-q4-r.csv")

# Subset by inconsistent vs. consistent scenarios
results1 <-subset(results,scen<4)
results2 <-subset(results,scen>3)

# Define the top and bottom of the errorbars
limits <- aes(ymax = u95, ymin=l95)

# Labels for time
results$time <- factor(results$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results1$time <- factor(results1$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))
results2$time <- factor(results2$time, levels=c('0','1'), labels=c("Raw data","Raw data + \n inequality graph"))

# Labels for scenario
results$scen <- factor(results$scen, levels=c('0','1','2','3','4','5'), labels=c('Difference decreases, ratio increases','Difference constant,ratio increases','Small diff. decrease,ratio constant','Large diff. decrease,ratio constant','Difference decreases,ratio decreases', 'Difference increases,ratio increases'))

results1$scen <- factor(results1$scen, levels=c('0','1','2','3','4','5'), labels=c('Difference decreases, \n ratio increases','Difference constant, \n ratio increases','Small diff. decrease, \n ratio constant','Large diff. decrease, \n ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

results2$scen <- factor(results2$scen, levels=c('0','1','2','3','4','5'), labels=c('Difference decreases, \n ratio increases','Difference constant, \n ratio increases','Small diff. decrease, \n ratio constant','Large diff. decrease, \n ratio constant','Difference decreases, ratio decreases', 'Difference increases, ratio increases'))

# All scenarios on single plot
ggplot(results, aes(fill=as.factor(trt), y=margin, x=as.factor(time))) + geom_bar(position="dodge", stat="identity", colour="black") + scale_y_continuous(limits=c(0,7)) + geom_errorbar(limits, position = position_dodge(width=0.9), width=0.2) + scale_fill_manual(name="Experimental Condition",values=c("#CCCCCC","#FFFFFF"),labels=c("Shown raw data and difference measure of inequality","Shown raw data and ratio measure of inequality")) + facet_wrap(~scen, nrow=2) + theme_bw() + theme(legend.position="bottom", legend.direction="vertical") + ylab("Should the program continue? \n 1(def. not) ... 7(very def. should)") + xlab("") + theme(axis.text.x = element_text(size = 9), axis.title.y=element_text(size=10, angle=90), axis.text.y = element_text(size = 9), strip.text = element_text(size=7))  

ggsave("q4.tiff", width=6, height=5, dpi=600)

